<?php
/**
 * Created by PhpStorm.
 * User: simaguo
 * Date: 2018/6/11
 * Time: 22:52
 */

namespace Simaguo\DS\Lists;


class LinkedList
{
    protected $head;

    public function __construct($headElem = null)
    {
        $this->createHead($headElem);
    }

    /**
     * 头插法
     * @param $num
     * @return LinkedList
     */
    public static function createListHead($num): self
    {
        $list = new self();
        $head = $list->head;
        for ($i = 0; $i < $num; $i++) {
            $firstNode = $head->next;
            $node = new Node($i.'-'.mt_rand(100, 999));
            $head->next = $node;
            if ($firstNode) {
                $node->next = $firstNode;
            }

        }

        return $list;
    }

    /**
     * 尾插法
     * @param $num
     * @return LinkedList
     */
    public static function createListTail($num): self
    {
        $list = new self();
        $currentNode = $list->head;
        for ($i = 0; $i < $num; $i++) {
            $node = new Node($i.'-'.mt_rand(100, 999));
            $currentNode->next = $node;
            $currentNode = $node;
        }

        return $list;
    }

    protected function createHead($headElem)
    {
        if (is_null($this->head)) {
            $this->head = new Node($headElem);
        }
    }

    public function insert($index, Node $node)
    {
        $i = 0;
        $currentNode = $this->head;
        while ($i < $index && !is_null($currentNode->next)) {
            $i++;
            $currentNode = $currentNode->next;
        }
        if ($i + 1 === $index) {
            //原先index处的节点,当前节点在index-1处;
            $originNode = $currentNode->next;
            $currentNode->next = $node;
            $node->next = $originNode;

            return true;
        }

        return false;
    }

    public function get($index)
    {
        $i = 0;
        $currentNode = $this->head;
        while ($i < $index && !is_null($currentNode->next)) {
            $i++;
            $currentNode = $currentNode->next;
        }

        if ($i > 0 && $i === $index) {
            return $currentNode->elem;
        }

        return false;
    }


    public function delete($index)
    {
        $i = 0;
        $currentNode = $this->head;
        while ($i < $index && !is_null($currentNode->next)) {
            $i++;
            $currentNode = $currentNode->next;
        }
        if ($i + 1 === $index) {
            //原先index处的节点,当前节点在index-1处;
            $originNode = $currentNode->next;
            $currentNode->next = $originNode->next;

            return $originNode->elem;
        }

        return false;
    }

    public function destroy()
    {
        $currentNode = $this->head;
        while ($currentNode instanceof Node) {
            $tmpNode = $currentNode->next;
            unset($currentNode->next);
            unset($currentNode->elem);
            unset($currentNode);
            $currentNode = $tmpNode;
        }

        return $this;
    }

    public function convertArray()
    {
        $array = array();
        $node = $this->head;
        $i = 0;
        while ($node->next) {
            $i++;
            $node = $node->next;
            $array[$i] = $node->elem;
        }

        return $array;
    }
}